Skip to content

Add external transaction support to allow Casbin and GORM to share a single DB transaction#1707

Open
Copilot wants to merge 3 commits intomasterfrom
copilot/combine-casbin-gorm-transactions
Open

Add external transaction support to allow Casbin and GORM to share a single DB transaction#1707
Copilot wants to merge 3 commits intomasterfrom
copilot/combine-casbin-gorm-transactions

Conversation

Copy link
Contributor

Copilot AI commented Feb 21, 2026

  • Add isExternal bool field to Transaction struct to indicate externally-managed DB transactions
  • Modify Commit() to skip DB commit/rollback when transaction is external
  • Modify Rollback() to skip DB rollback when transaction is external
  • Add BeginTransactionWithContext() to TransactionalEnforcer for joining an existing external transaction context
  • Add WithExternalTransaction() to TransactionalEnforcer as a convenience wrapper
  • Add tests covering the new external transaction functionality
  • Fix govet shadow linter error: rename err to addErr inside closure in TestWithExternalTransaction
Original prompt

This section details on the original issue you should resolve

<issue_title>casbin and gorm in the same tx</issue_title>
<issue_description>Casbin has it's own tx API. e.g:

// update roles, in tx
err = ef.WithTransaction(ctx, func(tx *casbin.Transaction) error {
	for _, rule := range addRules { // add
		_, err = tx.AddGroupingPolicy(deptCasbinKey, rule)
		if err != nil {
			return err
		}
		numPolicyUpdated++
	}
	for _, rule := range deleteRules { // delete
		_, err = tx.RemoveGroupingPolicy(deptCasbinKey, rule)
		if err != nil {
			return err
		}
		numPolicyUpdated++
	}

	return nil
})
if err != nil {
	return 0, apperr.WithStack(err)
}

gorm has it's own tx api too:

err := DB.Transaction(func(tx *gorm.DB) error {
	// ...
	return nil
})
if err != nil {
	lg.Error("test tx failed", zap.Error(err))
}

I have some operations involve both gorm and casbin operations, e.g delete a role and its permission from casbin;
Now I've to do it in 2 tx, 1 for gorm, 1 for casbin.
The 2 can't operate in the same tx, even casbin share the same connection pool with gorm.

Any idea how to make the whole operation in the same tx ?</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hsluoyz hsluoyz marked this pull request as ready for review February 21, 2026 09:25
…integration

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Integrate casbin and gorm in a single transaction Add external transaction support to allow Casbin and GORM to share a single DB transaction Feb 21, 2026
Copilot AI requested a review from hsluoyz February 21, 2026 09:29
@hsluoyz
Copy link
Member

hsluoyz commented Feb 21, 2026

@copilot fix go linter

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 21, 2026

@copilot fix go linter

Fixed in f0f0a12. The govet shadow linter flagged a variable err declared inside the WithExternalTransaction closure that shadowed the outer err. Renamed it to addErr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

casbin and gorm in the same tx

3 participants